<template>
  <el-dialog :visible="isShow" title="分配权限" @close="closeEvent">
    <el-tree
      ref="tree"
      node-key="id"
      :default-checked-keys="permIds"
      check-strictly
      :data="treeData"
      :props="{ label: 'name' }"
      show-checkbox
    ></el-tree>

    <template>
      <div>
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit"> 确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermission } from '@/api/permission'
import { changeTreeData } from '@/utils/index.js'
import { sysRoleGet, sysRolePut } from '@/api/setting'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      treeData: [],
      permIds: [],
      info: {}
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await sysPermission()
      this.treeData = changeTreeData(res.data, '0')
    },
    closeEvent () {
      this.$emit('update:isShow', false)
      //清空默认勾选
      this.$refs.tree.setCheckedKeys([])
    },
    //获取角色详情
    async getRoleInfo (id) {
      const res = await sysRoleGet(id)
      this.info = res.data
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log('角色详情', res)
    },
    //提交点击
    async submit(){
        await sysRolePut({
            ...this.info,
            permIds:this.$refs.tree.getCheckedKeys()
        })
        this.$message.success('更新权限成功')
        this.closeEvent()
        console.log(this.$refs.tree.getCheckedKeys());
    }
  }
}
</script>

<style></style>
